home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* AAChStk *}
- {* Copyright (c) Julian M Bucknall 1998-1999 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Character stack *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit AAChStk;
-
- interface
-
- uses
- SysUtils;
-
- type
- TaaCharacterStack = class
- private
- FStack : PChar;
- FSP : integer;
- FSize : integer;
- protected
- public
- constructor Create;
- {-create the character stack}
- destructor Destroy; override;
- {-destroy the character stack; releasing all memory}
-
- procedure Clear;
- {-remove all characters from stack}
- function Count : integer;
- {-count of characters on the stack}
- function Examine : char;
- {-return the top character from the stack; don't pop it}
- function IsEmpty : boolean;
- {-is the stack empty?}
- function Pop : char;
- {-pop the top character from the stack; return it}
- procedure Push(aCh : char);
- {-push the given character onto the stack}
- end;
-
- implementation
-
- {===TaaCharacterStack===================================================}
- constructor TaaCharacterStack.Create;
- begin
- inherited Create;
- FSize := 1024;
- FStack := StrAlloc(FSize);
- FSP := -1;
- end;
- {--------}
- destructor TaaCharacterStack.Destroy;
- begin
- if (FStack <> nil) then
- StrDispose(FStack);
- inherited Destroy;
- end;
- {--------}
- procedure TaaCharacterStack.Clear;
- begin
- FSP := -1;
- end;
- {--------}
- function TaaCharacterStack.Count : integer;
- begin
- Result := succ(FSP);
- end;
- {--------}
- function TaaCharacterStack.Examine : char;
- begin
- {check for the obvious mistake}
- if (FSP = -1) then
- raise Exception.Create('TaaCharacterStack.Examine: the stack is empty');
- {return the current character}
- Result := FStack[FSP];
- end;
- {--------}
- function TaaCharacterStack.IsEmpty : boolean;
- begin
- Result := (FSP = -1);
- end;
- {--------}
- function TaaCharacterStack.Pop : char;
- begin
- {check for the obvious mistake}
- if (FSP = -1) then
- raise Exception.Create('TaaCharacterStack.Pop: the stack is empty');
- {return the current character}
- Result := FStack[FSP];
- {decrement the stack pointer}
- dec(FSP);
- end;
- {--------}
- procedure TaaCharacterStack.Push(aCh : char);
- var
- NewSize : integer;
- NewStack : PChar;
- begin
- {increment the stack pointer}
- inc(FSP);
- {if we've run out of space, reallocate the stack buffer}
- if (FSP = FSize) then begin
- NewSize := FSize + 256;
- NewStack := StrAlloc(NewSize);
- Move(FStack^, NewStack^, FSize);
- StrDispose(FStack);
- FStack := NewStack;
- FSize := NewSize;
- end;
- {store the pushed character}
- FStack[FSP] := aCh;
- end;
- {====================================================================}
-
- end.
-